$(".return").click(function () {
    var pageSize = localStorage['pageSize'];
    var pageNumber = localStorage['pageNumber'];
    var url = "/page/" + localStorage['role'] + "/studentList";
    $("#body").load(url);
    setTimeout(function () {
        load($(".list"), 1500);
        addData_studentList(parseInt(pageSize), parseInt(pageNumber));
    }, 100);
});

var add_Student_data = function () {
    addCollege();
    addDorm();
    $.ajax({
        url: "student/findOne",
        type: "post",
        data: {
            "token": localStorage['token'],
            "id": localStorage['studentId']
        },
        success: function (data) {
            console.log(data);
            if (data.code == 200) {
                var student = data.data;
                // console.log(student);
                $("#sid").val(student.id);
                $("#name").val(student.name);
                $("#qq").val(student.qq);
                $("#phone").val(student.phone);
                $("#college").val(student.college);
                $("#class").val(student.clas);
                $("#tungNum").val(student.tungNum);
                $("#roomNum").val(student.roomNum);
                $("#bunkNum").val(student.bunkNum);

            } else {
                message(data.msg, "请重新尝试或者联系管理员", "error", 1500);
            }
        }
    });

};

var addCollege = function () {
    $.ajax(
        {
            url: "college",
            type: "post",
            data: {
                "token": localStorage['token']
            },
            success: function (data) {
                $("#college").next().next().next().children("ul").text("");
                if (data.code === 200) {
                    for (var i = 0; i < data.data.length; i++) {
                        var college = data.data[i];
                        var li = addOption(college.name, college.id);
                        li.appendTo($("#college").next().next().next().children("ul"));

                    }
                } else {
                    message(data.msg, "请尝试尝试或者联系管理员", "error", 1500);
                }
            }
        }
    )
};

var addDorm = function () {
    $.ajax({
        url: "dorm/tungAndRoom",
        type: "post",
        data: {
            "token": localStorage['token']
        },
        success: function (data) {
            $("#bunkNum").next().next().next().children("ul").text("");
            $("#tungNum").next().next().next().children("ul").text("");
            $("#roomNum").next().next().next().children("ul").text("");
            if (data.code === 200) {
                var tungs = data.data.tungs;
                var rooms = data.data.rooms;
                var bunks = data.data.bunks;
                for (var i = 0; i < rooms.length; i++) {
                    var room = rooms[i];
                    var li1 = addOption(room.num, room.id);
                    li1.appendTo($("#roomNum").next().next().next().children("ul"));

                }
                for (var j = 0; j < tungs.length; j++) {
                    var tung = tungs[j];
                    var li2 = addOption(tung.num, tung.id);
                    li2.appendTo($("#tungNum").next().next().next().children("ul")
                    )
                    ;
                }
                for (var k = 0; k < bunks.length; k++) {
                    var bunk = bunks[k];
                    // console.log(bunk);
                    var li3 = addOption(bunk, "");
                    li3.appendTo($("#bunkNum").next().next().next().children("ul"));

                }
            } else {
                message(data.msg, "请尝试尝试或者联系管理员", "error", 1500);

            }
        }
    })
};

// $(".SHselect-father").click(function (e) {
//     // console.log( $(".SHselect-son").children("ul").children("li"));
//     if (!$(this).parent().hasClass("notAllow")) {
//         $(this).children(".SHselect-son").slideToggle();
//         e.stopPropagation();
//     }
// });


$("#edit-student").click(function () {
    load(".student-detail", 500);
    $(this).addClass("hid");
    $(this).next().removeClass("hid");
    $("#tungNum").parent().parent().removeClass("notAllow");
    // $("#class").parent().removeClass("notAllow");
    // $("#class").removeAttr("disabled");
});


$('#udpate-student-admin').click(function () {
    var collegeId = "";
    if (!isNull($("#college").next().val())) {
        collegeId = $("#college").next().val();
    }
    var tungId = "";
    if (!isNull($("#tungNum").next().val())) {
        tungId = $("#tungNum").next().val();
    }
    var roomId = "";
    if (!isNull($("#roomNum").next().val())) {
        roomId = $("#roomNum").next().val();
    }
    var bunkNum = "";
    if (!isNull($("#bunkNum").val())) {
        bunkNum = $("#bunkNum").val();
    }
    $.ajax({
        url: "/student/update/admin",
        type: "post",
        data: {
            "roomId": roomId,
            "bunkNum": bunkNum,
            "tungId": tungId,
            "collegeId": collegeId,
            "clas": $("#class").val(),
            "id": $("#sid").val(),
            "token": localStorage['token']
        },
        success: function (data) {
            if (data.code === 200) {
                message("修改成功!", "", "success", 1500);
                load(".student-detail", 2000);
                // add_Student_data();
                afterUpdate();

            } else {
                message(data.msg, "请尝试尝试或者联系管理员", "error", 1500);
            }
        }
    })
});

var afterUpdate = function () {
    $("#udpate-student-admin").addClass("hid");
    $("#edit-student").removeClass("hid");
    // $("#class").parent().addClass("notAllow");
    $("#tungNum").parent().parent().addClass("notAllow");
    $("#bunkNum,#roomNum,#tungNum").removeClass("SHselect-success");
    // $("#class").parent().removeClass("input_success");

};